<?php
namespace Dyadmin\Controller;

use Think\Controller;

class IndexController extends CommomController
{

    public function index()
    {
        $this->check('1_3');
        $login_session = session('login_session');
        if ($login_session == NULL) {
            $this->redirect('/Dyadmin/login/index');
        }
        $admin = M('Admin')->where(array('login_session' => $login_session))->find();

        $data = S('data');
        if(empty($data)){
            $data['config_id'] = M('WebConfig')->count();
            $data['user'] = M('User')->count();
            $data['admin'] = M('Admin')->count();
            $data['amount'] = M('Order')->select();
            $data['order'] = M('Order')->count();
            S('data',$data,600);
        }
        $add = '';
        foreach ($data['amount'] as $v) {
            $add += $v['amount'];
        }

        //当日流水总额
        $where['status'] = 1;
        $start_time = strtotime(date('Y-m-d') . ' 0:0:0');//当天的最早时刻
        $end_time = strtotime('+1 day', $start_time) - 1;//当天的最晚时刻
        //egt 大于等于 elt小于等于
        $where['add_time'] = array(array('egt', $start_time), array('elt', $end_time));
        //缓存时间10分钟
        $same_day = S('same_day');
        if(empty($same_day)){
            $same_day = M('Order')->field('SUM(amount) AS sum_amount')->where($where)->find();
            S('same_day',$same_day,600);
        }
        //当月流水总额
        $where['status'] = 1;
        $month_start_time = strtotime(date('Y-m') . "-01 0:0:0");//当月的最早时刻
        $month_end_time = strtotime("+1 month", $month_start_time) - 1;//当月的最晚时刻
        //egt 大于等于 elt小于等于
        $where['add_time'] = array(array('egt', $month_start_time), array('elt', $month_end_time));
        //缓存时间2个小时
        $same_month = S('same_month');
        if(empty($same_month)){
            $same_month = M('Order')->field('SUM(amount) AS sum_amount')->where($where)->find();
            S('same_month',$same_month,7200);
        }
        $array['tj'] = $data;
        $this->assign('same_day', $same_day);
        $this->assign('same_month', $same_month);
        $this->assign($array);
        $this->assign('admin', $admin);
        $this->assign('add', $add);
        $this->display();
    }

    //站点首页
    public function homePage()
    {
        $login_session = session('login_session');
        $admin = M('Admin')->where(array('login_session' => $login_session))->find();
        $data = S('data');
        if(empty($data)){
            $data['config_id'] = M('WebConfig')->count();
            $data['user'] = M('User')->count();
            $data['admin'] = M('Admin')->count();
            $data['amount'] = M('Order')->select();
            $data['order'] = M('Order')->count();
            S('data',$data,600);
        }
        $add = '';
        foreach ($data['amount'] as $v) {
            $add += $v['amount'];
        }
        //当日流水总额
        $where['status'] = 1;
        $start_time = strtotime(date('Y-m-d') . ' 0:0:0');//当天的最早时刻
        $end_time = strtotime('+1 day', $start_time) - 1;//当天的最晚时刻
        //egt 大于等于 elt小于等于
        $where['add_time'] = array(array('egt', $start_time), array('elt', $end_time));
        //缓存时间10分钟
        $same_day = S('same_day');
        if(empty($same_day)){
            $same_day = M('Order')->field('SUM(amount) AS sum_amount')->where($where)->find();
            S('same_day',$same_day,600);
        }
        //当月流水总额
        $where['status'] = 1;
        $month_start_time = strtotime(date('Y-m') . "-01 0:0:0");//当月的最早时刻
        $month_end_time = strtotime("+1 month", $month_start_time) - 1;//当月的最晚时刻
        //egt 大于等于 elt小于等于
        $where['add_time'] = array(array('egt', $month_start_time), array('elt', $month_end_time));
        //缓存时间2个小时
        $same_month = S('same_month');
        if(empty($same_month)){
            $same_month = M('Order')->field('SUM(amount) AS sum_amount')->where($where)->find();
            S('same_month',$same_month,7200);
        }
        $array['tj'] = $data;
        $this->assign('same_day', $same_day);
        $this->assign('same_month', $same_month);
        $this->assign('add', $add);
        $this->assign($array);
        $this->assign('admin', $admin);
        $this->display();
    }
}